<?php
/*
 * @file
 * Theme file to handle HTML5 output.
 *
 * Variables passed.
 * $video is the video object.
 * $node is the node object.
 *
 * @TODO : Fallback to flash should be done nicely
 *
 */
?>
<!-- Using the Video for Everybody Embed Code http://camendesign.com/code/video_for_everybody -->
<?php if (false) { ?>
<video width="<?php echo $video->player_width; ?>" autobuffer="<?php print $video->autobuffering; ?>" height="<?php echo $video->player_height; ?>" controls="controls" preload="auto" poster="<?php echo $video->thumbnail->url; ?>">
  <?php //dd($items); ?>
  <?php static $videojs_sources; ?>
  <?php $codecs = array('video/mp4' => 'avc1.42E01E, mp4a.40.2', 'video/webm' => 'vp8, vorbis', 'video/ogg' => 'theora, vorbis', 'application/ogg' => 'theora, vorbis', 'video/ogv' => 'theora, vorbis', 'video/quicktime' => 'avc1.42E01E, mp4a.40.2'); ?>
  <?php foreach ($video->files as $filetype => $file): ?>
  <?php $filepath = $file->url; ?>
  <?php $mimetype = file_get_mimetype($file->filepath); ?>
  <?php if (array_key_exists($mimetype, $codecs)): ?>
  <?php $mimetype = ($mimetype == 'video/quicktime') ? 'video/mp4' : $mimetype; ?>
  <?php if ($mimetype == 'video/mp4' || $mimetype == 'video/flv')
        $flash = $filepath; ?>
  <?php $videojs_sources .= "<source src=\"$filepath\" type='$mimetype; codecs=\"" . $codecs[$mimetype] . "\"' />"; ?>
  <?php endif; ?>
  <?php endforeach; ?>
  <?php print $videojs_sources; ?>
      <!-- Flash Fallback. Use any flash video player here. Make sure to keep the vjs-flash-fallback class. -->
  <?php $video->player = 'flv'; ?>
  <?php $video->files->flv->url = $flash; ?>
  <?php echo theme('video_flv', array('video' => $video)); ?>
</video>
<?php } else { ?>
<?php 
  global $base_url;
  $filepath = '';
  foreach ($video->files as $filetype => $file) {
    $filepath = file_create_url($file->uri);
  }
  $r = rand();
  drupal_add_js(drupal_get_path('theme', 'govideo') . '/js/jwplayer/jwplayer.js'); 
?>


<div id='mediaspace<?php print $r; ?>'>JW Player goes here</div>

<?php //drupal_set_message( '<pre>'. check_plain(print_r($video, 1)) .'</pre>'); ?> 

<script type='text/javascript'>
  jwplayer('mediaspace<?php print $r; ?>').setup({
    'flashplayer': '<?php print $base_url.'/'.drupal_get_path('theme', 'govideo') ?>/js/jwplayer/player.swf',
    'file': '<?php print $filepath; ?>',
    'image': '<?php print $video->thumbnail->url; ?>',

    'author': '<?php print str_replace ("'", "\'",$video->node->name); ?>',
    'description': '<?php print str_replace ("'", "\'",govideo_truncate_utf8($video->node->body[$video->node->language][0]['value'], 250, true, true)); ?>',
    'title': '<?php print str_replace ("'", "\'",$video->node->title); ?>',
    'date': '<?php print format_date($video->node->created); ?>',

    <?php if ($video->autoplay) { ?> 
    'autostart': 'true',
    <?php } else { ?> 
    'autostart': 'false',
    <?php } ?>
    'icons': 'false',
    'quality': 'false',
    'stretching': 'fill',
    'controlbar': 'over',
    <?php if (isset($video->node->emplayer_width)) { ?> 
    'width': '<?php print $video->node->emplayer_width; ?>',
    'height': '<?php print $video->node->emplayer_height; ?>'
    <?php } else { ?> 
    'width': '675',
    'height': '438'
    <?php } ?>
  });
</script>

<?php } ?>